package parttern.memo;

import java.util.ArrayList;
import java.util.List;

//看守人
public class CareTaker {

    private List<Memento> mementoList = new ArrayList<>();

    public void addMemento(Memento memento) {
        mementoList.add(memento);
    }

    public Memento getMemento(int index) {
        //存在越界异常
        return mementoList.get(index);
    }


    ///test
    public static void main(String[] args) {
        Originator originator = new Originator();
        CareTaker careTaker = new CareTaker();
        //发起人干活并将状态交给看守人即保存在看守人的备忘录集合里
        originator.setState("state#1");
        originator.setState("state#2");
        careTaker.addMemento(originator.saveStateToMemento());
        originator.setState("state#3");
        careTaker.addMemento(originator.saveStateToMemento());
        originator.setState("state#4");
        careTaker.addMemento(originator.saveStateToMemento());

        System.out.println("当前状态:" + originator.getState());
        originator.getStateFromMemento(careTaker.getMemento(0));
        System.out.println("第一次存储状态:" + originator.getState());
        originator.getStateFromMemento(careTaker.getMemento(2));
        System.out.println("第三次存储状态:" + originator.getState());
        originator.getStateFromMemento(careTaker.getMemento(3));
        System.out.println("第四次存储状态:" + originator.getState());
    }

}
